--- title: "L2-035 完全二叉树的层序遍历" created: 2025-11-28 tags: - 算法 --- # L2-035 完全二叉树的层序遍历 ## 题目 [L2-035 完全二叉树的层序遍历](https://pintia.cn/problem-sets/994805046380707840/exam/problems/type/7?problemSetProblemId=1336215880692482058&page=1) ![[image-21b991cd.png]] ## 思路分析 ## 代码实现 ```cpp #include using namespace std; #define int long long #define endl '\n' using ll = long long; using ull = unsigned long long; using PII = pair; using Pll = pair; int dx[4]= {-1,0,1,0},dy[4]= {0,1,0,-1}; const int inf = 0x3f3f3f3f; priority_queue pq; multiset s; vector post; vector level; int n; int idx=1; void build_from_post(int root){ if(root>n) return; build_from_post(2*root); build_from_post(2*root+1); level[root]=post[idx++]; } void build_from_pre(int root){ if(root>n) return; level[root]=post[idx++]; build_from_post(2*root); build_from_post(2*root+1); } void build_from_in(int root){ if(root>n) return; build_from_post(2*root); level[root]=post[idx++]; build_from_post(2*root+1); } signed main() { ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); cin>>n; post.resize(n+1); level.resize(n+1); for(int i=1;i<=n;i++){ cin>>post[i]; } build_from_post(1); for(int i=1;i<=n;i++){ if(i>1) cout<<" "; cout<